<?php

class ProductDAO extends My_DAO
{
	/** Table name */
    protected $_name    = 'product';
    
	public function buildQueryData($orderBy = "", $orderDirection = "",
									 $key = "", $field = "", $params = null)
	{
		$select = parent::buildQueryData($orderBy, $orderDirection,$key,$field);
		
		$categoryId = $params[0];
		if(!empty($categoryId))
		{
			$select->where('product_category.categoryId = '.$categoryId);
		}
		$langId = My_Translate::getLanguage();
		$select->where('product_category.languageId = '.$langId);
		
		$select->joinLeft('product_category', 
					'product_category.categoryId=product.categoryId', array('categoryName'))
				->joinLeft('user', 'userId = createdBy', array('userName'));

		return $select;
	}
	
	public function getRandomProducts($num)
	{
		$select = parent::buildQueryData();
		$select->where('product.status = 1');
		$langId = My_Translate::getLanguage();
		$select->where('product_category.languageId = '.$langId);
		$select->joinLeft('product_category', 
					'product_category.categoryId=product.categoryId', array('categoryName'));
		$select->order("RAND()")->limit($num);
		return $select->query()->fetchAll();
		
	}
	
	public function getPublish($catId)
	{
		$select = parent::buildQueryData();
		if(empty($catId))
		{
			return null;
		}
		$select->where('product.status = 1');
		$select->where('product.categoryId = '.$catId);
		$langId = My_Translate::getLanguage();
		$select->where('product_category.languageId = '.$langId);
		$select->joinLeft('product_category', 
					'product_category.categoryId=product.categoryId', array('categoryName'));
		return $select;
	}
	
	public function getTopProducts()
	{
		$select = parent::buildQueryData();
		$select->where('product.status = 1');
		
		$langId = My_Translate::getLanguage();
		$select->where('product_category.languageId = '.$langId);
		$select->joinLeft('product_category', 
					'product_category.categoryId=product.categoryId', array('categoryName'));
		
		$select->limit(5);
		return $select->query()->fetchAll();
	}
	
}

